package com.xuecheng.web.exceptions;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.ResponseResult;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@Component
/*
* @RestControllerAdvice   声明自定义的全局异常处理器
* seata调用的请求不需要进行全局异常处理
* */
@RestControllerAdvice(
        basePackages = {
                "com.xuecheng.system.controller",
                "com.xuecheng.basic.controller",
                "com.xuecheng.content.controller",
                "com.xuecheng.learning.controller",
                "com.xuecheng.order.controller",
                "com.xuecheng.search.controller",
                "com.xuecheng.media.controller"
        }
)
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseResult runtimeException(Exception e) {
        e.printStackTrace();
        return ResponseResult.errorResult(ErrorCode.ERROR);//返回异常信息
    }

    @ExceptionHandler(BusinessException.class)  //处理自定义业务异常
    public ResponseResult businessException(BusinessException e) {
        e.printStackTrace();
        return ResponseResult.errorResult(e.getErrorCode());//返回业务异常信息
    }
}
